package in.iqing.control.util;

import android.content.Context;
import android.graphics.Point;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.TextUtils;
import android.view.WindowManager;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by neavo on 2014/8/7.
 */

public class MiscUtil {

    public static synchronized float getDPI(Context ctx) {
        return ctx.getResources().getDisplayMetrics().density;
    }

    public static synchronized String getNetType(Context ctx) {
        String result = "UNKNOWN";

        if (ctx != null) {
            ConnectivityManager manager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();

            if (networkInfo != null && networkInfo.isAvailable()) {
                int type = networkInfo.getType();

                switch (type) {
                    case ConnectivityManager.TYPE_WIFI:
                        result = "WiFi";
                        break;
                    case ConnectivityManager.TYPE_MOBILE:
                        result = "Mobile";
                        break;
                }
            }
        }

        return result;
    }

    public static synchronized boolean isNetworkConnection(Context ctx) {
        NetworkInfo info = (
                (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)
        ).getActiveNetworkInfo();

        return info != null && info.isAvailable();
    }

    public static synchronized int getScreenWidth(Context ctx) {
        Point point = new Point();

        WindowManager manager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getSize(point);

        return point.x;
    }

    public static synchronized int getScreenHeight(Context ctx) {
        Point point = new Point();

        WindowManager manager = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
        manager.getDefaultDisplay().getSize(point);

        return point.y;
    }

    public static boolean isValidMobile(String mobiles) {
        boolean result = false;
        try {
            Pattern p = Pattern.compile("^1[3|5|7|8][0-9]\\d{8}$");
            Matcher m = p.matcher(mobiles);
            result = m.matches();
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    /**
     * 6-24个字节，仅允许中文、日文、英文、数字和"-"
     *
     * @param username
     * @return is valid username
     */
    public static boolean isValidUsername(String username) {
        // \u4e00-\u9fa5 (中文)
        // \u0800-\u4e00 (日文)
        if (TextUtils.isEmpty(username)) {
            return false;
        }
        if (username.length() < 2 || username.length() > 24) {
            return false;
        }

        boolean result = false;
        try {
            Pattern p = Pattern.compile("^[-a-z0-9A-Z\\u4e00-\\u9fa5\\u0800-\\u4e00]+$");
            Matcher m = p.matcher(username);
            result = m.matches();
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    public static boolean isValidVerifyCode(String code) {
        if (TextUtils.isEmpty(code)) {
            return false;
        }
        return code.length() == 6;
    }

    public static boolean isValidPassword(String password) {
        // 允许英文、数字和英文符号（ASCII=[33, 126]）
        if (TextUtils.isEmpty(password)) {
            return false;
        }
        if (password.length() < 2 || password.length() > 24) {
            return false;
        }

        boolean result = false;
        try {
            Pattern p = Pattern.compile("^[-a-z0-9A-Z\\x21-\\x7E]+$");
            Matcher m = p.matcher(password);
            result = m.matches();
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }
        char[] charArray = inStr.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++)
            byteArray[i] = (byte) charArray[i];
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16)
                hexValue.append("0");
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();

    }

}
